package com.backend.service.Impl;

import com.backend.config.Result;
import com.backend.entity.AssInformation;
import com.backend.mapper.AssInforMapper;
import com.backend.service.AssInforService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AssInforServiceImpl  implements AssInforService {

    @Autowired
    AssInforMapper inforMapper;

    @Override
    public Result<List<AssInformation>> lookUpAllAss(Integer id) {


        List<AssInformation> informations=inforMapper.lookUpAllAss(id);
        return Result.success("查询成功",informations);
    }

    @Override
    public Result getAllAss(String userId, String assId) {
        List<Map<String,String>> mapList=inforMapper.getAllAss(assId);
        for(Map<String,String> map:mapList){
            Map<String,String> ifUser=inforMapper.ifwhether(String.valueOf(map.get("id")),userId);
            if(ifUser!=null){
                map.put("whether",ifUser.get("def"));
            }else {
                map.put("whether","-1");
            }
        }
        return Result.success("查找成功",mapList);
    }

    @Override
    public Result getAllAssByAdmin() {
        List<Map<String,String>> mapList=inforMapper.getAllAssByAdmin();
        return Result.success("查找成功",mapList);
    }

    @Override
    public Result JoinAss(String userId, String assId) {
        return Result.success("插入成功",inforMapper.JoinAss(userId,Integer.valueOf(assId)));
    }

    @Override
    public Result lookUpAllAssAdmin(com.backend.common.PageHelper pageHelper) {
        PageHelper.startPage(pageHelper.getPageNum(),pageHelper.getPageSize());
        List<AssInformation> informations=inforMapper.lookUpAllAss(pageHelper.getId());
        return Result.success("查询成功",informations);
    }


    @Override
    public Result lookUpAssByName(String assName) {
        List<AssInformation> informationList=inforMapper.lookUpAssByName(assName);
        return Result.success("查询成功",informationList);
    }

    @Override
    public Result getPageTotle(Integer pageSize) {
        Map<String,String> map=inforMapper.getPageTotle();
        int totle=Integer.parseInt(String.valueOf(map.get("totle")));
        if(totle%pageSize==0){
            return Result.success("查询成功",totle/pageSize);
        }
        return Result.success("查询成功",(totle/pageSize)+1);


    }
}
